package com.ligongmusic.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.ligongmusic.R;
import com.ligongmusic.domain.DynamicDomain;
import com.ligongmusic.util.GlobalParams;
import com.ligongmusic.util.JsonUtils;
import com.ligongmusic.util.NetWorkUtils;
import com.ligongmusic.util.StringUtils;
import com.ligongmusic.view.TopManager;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/19.
 */
public class WriteComment extends Activity {

    private DynamicDomain info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.write_comment);
        info = (DynamicDomain) getIntent().getSerializableExtra("info");
        TopManager.getTopManager(this).showTop(true, "写评论", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ((Button)findViewById(R.id.send_comments)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = ((EditText) findViewById(R.id.et_write_comment)).getText().toString().trim();
                if (StringUtils.isEmpty(content)){
                    Toast.makeText(WriteComment.this, "评论内容不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                writeComments(info.getSong_id(), content);
            }
        });
    }

    private void writeComments(int sid, String commentContent) {
        List<NameValuePair> pairs = new ArrayList<NameValuePair>();
        pairs.add(new BasicNameValuePair("token", GlobalParams.token));
        pairs.add(new BasicNameValuePair("songid", sid + ""));
        pairs.add(new BasicNameValuePair("content", URLEncoder.encode(commentContent)));
        pairs.add(new BasicNameValuePair("loginname", GlobalParams.userName));
        NetWorkUtils.getRequest(pairs, GlobalParams.WRITE_COMMENTS, new NetWorkUtils.NetWorkCallBack() {
            @Override
            public void success(String result) {
                Toast.makeText(WriteComment.this, JsonUtils.getString(result, "message"), Toast.LENGTH_SHORT).show();
                if ("200".equals(JsonUtils.getString(result, "code"))) {
                    //评论成功，关闭界面，跳转到评论页面
                    Intent intent = new Intent(WriteComment.this, CommentsActivity.class);
				    intent.putExtra("comments", info);
				    startActivity(intent);
                    finish();
                }
            }

            @Override
            public void error(String message) {
                Toast.makeText(WriteComment.this, "评论失败，请重试", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void error() {// 网络连接异常及服务器异常
                Toast.makeText(WriteComment.this, "评论失败，请检查网络设置", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
